package com.moon.interface_api.service.serviceImpl;

import com.aliyuncs.exceptions.ClientException;
import com.moon.interface_api.constansts.MsgConstants;
import com.moon.interface_api.constansts.SmsConstants;
import com.moon.interface_api.response.BaseResponse;
import com.moon.interface_api.service.LoginService;
import com.moon.interface_api.utils.RandomCode;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * @ClassName: LoginServiceImpl
 * @Description: 登录接口实现
 *Created by chengliang on 2018/11/6.
 */
@Service
public class LoginServiceImpl implements LoginService {

	private static final int timeout = 24 * 60 * 60 * 30;

	private static final int codetimeout = 10 * 60;
	
	@Autowired
	private RedisTemplate redisTemplate;
	

	/* 
	* <p>Title: sendCode</p>
	* <p>Description: 发送验证码</p>
	* @param codeRequest
	* @return
	* @see jc.xunjiedai.service.LoginService#sendCode(jc.xunjiedai.message.request.CodeRequest)
	*/ 
	/*@Override
	public BaseResponse sendCode(Map<String, String> params) {
		BaseResponse baseResponse = new BaseResponse();
		
		if (("login").equals(params.get("type"))
				||("update").equals(params.get("type"))
				||("register").equals(params.get("type"))
				||("bankcode").equals(params.get("type"))
				||("tixiancode").equals(params.get("type"))) {
		
			String code = RandomCode.getRandNum();
			AliSms aliSms = new AliSms();
			//组装请求对象
			SendSmsRequest request = new SendSmsRequest();
			//必填:待发送手机号
			request.setPhoneNumbers(params.get("phoneNum"));
			//必填:短信签名
			request.setSignName(SmsConstants.SMS_SIGN_NAME);
			//必填:短信模板
			request.setTemplateCode(SmsConstants.SMS_TEMPLATE_CODE);
			//可选:模板中的变量替换JSON串,如模板内容为"您的验证码为${code}"时,此处的值为
			request.setTemplateParam("{\"code\":\""+code+"\"}");
	
			//选填-上行短信扩展码(无特殊需求用户请忽略此字段)
			//request.setSmsUpExtendCode("90997");
	
			//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
			request.setOutId("yourOutId");
			try {
				SendSmsResponse response = aliSms.sendSms(request);
				if (response!=null) {
					switch (response.getCode()) {
					case "OK":
						baseResponse.setCode(MsgConstants.SUCCESS_CODE);
						baseResponse.setMessage(MsgConstants.SUCCESS);
						//将验证码set到redis里面
						if (params.get("type").equals("login")) {
							redisTemplate.opsForValue().set(MsgConstants.PREFIX_LOGIN+params.get("phoneNum"), code, codetimeout, TimeUnit.SECONDS);
							//redisTemplate.setValue(MsgConstants.PREFIX_LOGIN+params.get("phoneNum"), codetimeout, code);
						}else if (params.get("type").equals("register")){
							redisTemplate.opsForValue().set(MsgConstants.PREFIX_REGISTER+params.get("phoneNum"), code, codetimeout,TimeUnit.SECONDS);
							//jedisClient.setValue(MsgConstants.PREFIX_REGISTER+params.get("phoneNum"), codetimeout, code);
						}else if (params.get("type").equals("update")){
							redisTemplate.opsForValue().set(MsgConstants.PREFIX_UPDATE+params.get("phoneNum"), code, codetimeout,TimeUnit.SECONDS);
							//jedisClient.setValue(MsgConstants.PREFIX_UPDATE+params.get("phoneNum"), codetimeout, code);
						}else if (params.get("type").equals("bankcode")){
							redisTemplate.opsForValue().set(MsgConstants.PREFIX_BANK_CODE+params.get("phoneNum"), code, codetimeout,TimeUnit.SECONDS);
							//jedisClient.setValue(MsgConstants.PREFIX_BANK_CODE+params.get("phoneNum"), codetimeout, code);
						}else if (params.get("type").equals("tixiancode")){
							redisTemplate.opsForValue().set(MsgConstants.PREFIX_TIXIAN_CODE+params.get("phoneNum"), code, codetimeout,TimeUnit.SECONDS);
							//jedisClient.setValue(MsgConstants.PREFIX_TIXIAN_CODE+params.get("phoneNum"), codetimeout, code);
						}
						break;
					case "isp.RAM_PERMISSION_DENY":
						baseResponse.setCode(MsgConstants.RAM_PERMISSION_DENY);
						baseResponse.setMessage(MsgConstants.MESSAGE_ERROR);
						break;
					case "isv.OUT_OF_SERVICE":
						baseResponse.setCode(MsgConstants.OUT_OF_SERVICE);
						baseResponse.setMessage(MsgConstants.MESSAGE_ERROR);
						break;	
					case "isv.PRODUCT_UN_SUBSCRIPT":
						baseResponse.setCode(MsgConstants.PRODUCT_UN_SUBSCRIPT);
						baseResponse.setMessage(MsgConstants.MESSAGE_ERROR);
						break;
					case "isv.PRODUCT_UNSUBSCRIBE":
						baseResponse.setCode(MsgConstants.PRODUCT_UNSUBSCRIBE);
						baseResponse.setMessage(MsgConstants.MESSAGE_ERROR);
						break;
					case "isv.ACCOUNT_NOT_EXISTS":
						baseResponse.setCode(MsgConstants.ACCOUNT_NOT_EXISTS);
						baseResponse.setMessage(MsgConstants.MESSAGE_ERROR);
						break;
					case "isv.ACCOUNT_ABNORMAL":
						baseResponse.setCode(MsgConstants.ACCOUNT_ABNORMAL);
						baseResponse.setMessage(MsgConstants.MESSAGE_ERROR);
						break;
					case "isv.SMS_TEMPLATE_ILLEGAL":
						baseResponse.setCode(MsgConstants.SMS_TEMPLATE_ILLEGAL);
						baseResponse.setMessage(MsgConstants.MESSAGE_ERROR);
						break;
					case "isv.SMS_SIGNATURE_ILLEGAL":
						baseResponse.setCode(MsgConstants.SMS_SIGNATURE_ILLEGAL);
						baseResponse.setMessage(MsgConstants.MESSAGE_ERROR);
						break;
					case "isv.INVALID_PARAMETERS":
						baseResponse.setCode(MsgConstants.INVALID_PARAMETERS);
						baseResponse.setMessage(MsgConstants.MESSAGE_ERROR);
						break;
					case "isp.SYSTEM_ERROR":
						baseResponse.setCode(MsgConstants.SYSTEM_ERROR);
						baseResponse.setMessage(MsgConstants.MESSAGE_YICHANG);
						break;
					case "isv.MOBILE_NUMBER_ILLEGAL":
						baseResponse.setCode(MsgConstants.MOBILE_NUMBER_ILLEGAL);
						baseResponse.setMessage(MsgConstants.MESSAGE_PHONE);
						break;
					case "isv.MOBILE_COUNT_OVER_LIMIT":
						baseResponse.setCode(MsgConstants.MOBILE_COUNT_OVER_LIMIT);
						baseResponse.setMessage(MsgConstants.MESSAGE_YICHANG);
						break;
					case "isv.TEMPLATE_MISSING_PARAMETERS":
						baseResponse.setCode(MsgConstants.TEMPLATE_MISSING_PARAMETERS);
						baseResponse.setMessage(MsgConstants.MESSAGE_ERROR);
						break;
					case "isv.BUSINESS_LIMIT_CONTROL":
						baseResponse.setCode(MsgConstants.BUSINESS_LIMIT_CONTROL);
						baseResponse.setMessage(MsgConstants.MESSAGE_ERROR);
						break;
					case "isv.INVALID_JSON_PARAM":
						baseResponse.setCode(MsgConstants.INVALID_JSON_PARAM);
						baseResponse.setMessage(MsgConstants.MESSAGE_ERROR);
						break;
					case "isv.BLACK_KEY_CONTROL_LIMIT":
						baseResponse.setCode(MsgConstants.BLACK_KEY_CONTROL_LIMIT);
						baseResponse.setMessage(MsgConstants.MESSAGE_ERROR);
						break;
					case "isv.PARAM_LENGTH_LIMIT":
						baseResponse.setCode(MsgConstants.PARAM_LENGTH_LIMIT);
						baseResponse.setMessage(MsgConstants.MESSAGE_ERROR);
						break;
					case "isv.PARAM_NOT_SUPPORT_URL":
						baseResponse.setCode(MsgConstants.PARAM_NOT_SUPPORT_URL);
						baseResponse.setMessage(MsgConstants.MESSAGE_ERROR);
						break;
					case "isv.AMOUNT_NOT_ENOUGH":
						baseResponse.setCode(MsgConstants.AMOUNT_NOT_ENOUGH);
						baseResponse.setMessage(MsgConstants.MESSAGE_ERROR);
						break;
					default:
						baseResponse.setCode(MsgConstants.OTHER);
						baseResponse.setMessage(MsgConstants.MESSAGE_ERROR);
						break;
					}
				}
			} catch (ClientException e) {
				e.printStackTrace();
			}
		}else {
			baseResponse.setCode(MsgConstants.ERROR_CODE);
			baseResponse.setMessage(MsgConstants.ERROR);
		}
		return baseResponse;
	}*/

	@Override
	public boolean checkToken(String phoneNum, String token) {
		String lingpai = redisTemplate.opsForValue().get("token_" + phoneNum).toString();
		if(StringUtils.isEmpty(lingpai)){
			return false;
		}
		if(token.equals(lingpai)){
			return true;
		}
		return false;
	}

}
